<?php

//广告控制器
class Ads extends Controller {

	function Ads()
	{
		parent::Controller();
		$this->load->model('ads_model');
	}
	
	//js获取广告
	function get()
	{
		$adstring = "";
		//广告位ID
		$placeid = u(3);
		if(intval($placeid) == 0) return false;
		$ad = $this->ads_model->get($placeid);
		
		if (is_array($ad) && count($ad)>0) {
			//更新views
			$this->ads_model->setviews($ad['adsid']);
			$adstring = "<a href='".$this->setlink($ad['adsid'], $ad['linkurl'])."' target='_blank'><img width='".$ad['place']['width']."' height='".$ad['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad['imageurl']."'></a>";
		}
		header('Content-Type: text/javascript');
		if(isset($ad['place']) && $ad['place']['height'] > 0) {
			$adstring = '<div class="ad'.$ad['place']['height'].'">'.$adstring.'</div>'; 
		}
		echo "document.write('".jsoutputformat($adstring)."');";exit;
	}
	
	//js获取广告2
	function get2()
	{
		$adstring = "";
		//广告位ID
		$placeid = u(3);
		$siteid = u(4);
		if(intval($placeid) == 0) return false;
		$ad = $this->ads_model->get($placeid, $siteid);
		if (is_array($ad) && count($ad)>0) {
			//更新views
			$this->ads_model->setviews($ad['adsid']);
			$adstring = "<a href='".$this->setlink($ad['adsid'], $ad['linkurl'])."' target='_blank'><img width='".$ad['place']['width']."' height='".$ad['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad['imageurl']."'></a>";
		}
		header('Content-Type: text/javascript');
		echo "document.write('".jsoutputformat($adstring)."');";exit;
	}
	
	//js获取广告
	function get3($jsoutput = true)
	{
		$adstring = "";
		//广告位ID
		$placeid = u(3);
		if(intval($placeid) == 0) return false;
		$ad = $this->ads_model->get($placeid);
		
		if (is_array($ad) && count($ad)>0) {
			//更新views
			$this->ads_model->setviews($ad['adsid']);
			$adstring = "<a href='".$this->setlink($ad['adsid'], $ad['linkurl'])."' target='_blank'><img width='".$ad['place']['width']."' height='".$ad['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad['imageurl']."'></a>";
		}
		if($jsoutput) {
			header('Content-Type: text/javascript');
			echo "document.write('".jsoutputformat($adstring)."');";exit;
		} else {
			return $adstring;
		}
	}
	
	//设置点击
	function setlink($adsid, $linkurl)
	{
		$linkurl = gurl('/ads/link/?url='.$linkurl.'&adsid='.$adsid);
		return $linkurl;
	}
	
	//广告链接
	function link()
	{
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		//更新clicks
		$adsid = $this->param["adsid"];
		$this->ads_model->setclicks($adsid);
		//跳转
		redirect($this->param["url"]);
	}
}

/* End of file */